package jp.bgbt.fles.app;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.common.utils.LogUtil;
import com.common.utils.MPermissionUtils;
import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import jp.bgbt.fles.app.bean.EventMsg;
import jp.bgbt.fles.app.bluetooth.BleCommandUtil;
import jp.bgbt.fles.app.bluetooth.BleConstant;
import jp.bgbt.fles.app.bluetooth.BleDeviceInfo;
import jp.bgbt.fles.app.rnmodule.BleModule;
import jp.bgbt.fles.app.wifi.WifiStateReceiver;
import com.reactlibrary.rnwifi.RNWifiPackage;

public class MainActivity extends ReactActivity {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "demo";
  }

  private WifiStateReceiver receiver = new WifiStateReceiver();



  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventBus.getDefault().register(this);
    init();

  }

  private void init(){
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(receiver, filter);
  }



  @Subscribe(threadMode = ThreadMode.MAIN)
  public void onGetEvent(EventMsg eventMsg){
    LogUtil.d("EventMsg" + eventMsg.getMsgType());
    WritableMap param = Arguments.createMap();
    switch (eventMsg.getMsgType()) {
      case EventMsg.MSG_BLE_CONNECTED:
        BleCommandUtil.getInstance().sendCommand(BleConstant.readUsedTime);
        param.putString("event","MSG_BLE_CONNECTED");
        break;
      case EventMsg.MSG_RANDOM_RECEIVED:
        BleModule.getInstance().openDeviceWifiAndGetWifiName();
        param.putString("event","MSG_RANDOM_RECEIVED");
        break;
      case EventMsg.MSG_WIFI_CONNECTED:
        param.putString("event","MSG_WIFI_CONNECTED");
        break;
      case EventMsg.MSG_WIFI_ADDR_RECEIVED:
        //暂停wifi连接倒计时
        param.putString("event","MSG_WIFI_ADDR_RECEIVED");
        BleModule.getInstance().startScanDeviceWifiAndConnect();
        break;
      case EventMsg.MSG_WIFI_DISCONNECTED:
        param.putString("event","MSG_WIFI_DISCONNECTED");
        break;
      case EventMsg.MSG_BLE_DISCONNECTED:
        param.putString("event","MSG_BLE_DISCONNECTED");
        break;
      case EventMsg.MSG_AUTO_CONNECT_FAILED:
        //连接失败
          param.putString("event","MSG_AUTO_CONNECT_FAILED");
        break;
      case EventMsg.MSG_TEST_AGAIN:
        param.putString("event","MSG_TEST_AGAIN");
        break;
      case EventMsg.BLE_133:
        param.putString("event","BLE_133");
        break;
      case EventMsg.MSG_DEVICE_VERSION:
        param.putString("event","MSG_DEVICE_VERSION");
        break;
      case EventMsg.MSG_TOTAL_RUNTIME:
        param.putString("event","MSG_TOTAL_RUNTIME");
        break;
      case EventMsg.MSG_OPEN_WIFI_SUCCESS:
        param.putString("event","MSG_OPEN_WIFI_SUCCESS");
        break;
      case EventMsg.NURSE_STATE_CHANGED:
        param.putString("event","NURSE_STATE_CHANGED");
        break;
    }

    BleModule.getInstance().senEvent(param);
  }




  @Override
  protected void onResume() {
    super.onResume();
    if(BleDeviceInfo.getInstance().isDeviceConnected() ){
      BleCommandUtil.getInstance().sendCommand(BleConstant.closeWifi);
      BleDeviceInfo.getInstance().setDeviceConnected(false);
    }
    BleDeviceInfo.getInstance().setInAnalysisResult(false);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
    unregisterReceiver(receiver);
  }
}
